{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "procedure c,pascal\n",
    "oop(object oriented programming) c++,python,java\n",
    "\n",
    "animal,plant\n",
    "animal: tiger,wolf,dog,...person\n",
    "plant: tree,...\n",
    "class,instance,\n",
    "封装(encapsulation)继承(extend)多态(Polymorphism)\n",
    "uml:UML建模技术是一种建模语言，指用模型元素来组建整个系统的模型，模型元素包括系统中的类、类和类之间的关联、类的实例相互配合实现系统的动态行为等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Dict\n",
    "\n",
    "\n",
    "class Animal(object):\n",
    "    #age = 0\n",
    "    #gender = 'male'\n",
    "    name = 'Animal'\n",
    "    def __init__(self, age, gender):\n",
    "        self.age = age\n",
    "        self.gender = gender\n",
    "    \n",
    "    @staticmethod\n",
    "    def can_move(a, b):\n",
    "        if a > b:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    def eat(self):\n",
    "        pass\n",
    "\n",
    "class Tiger(Animal):\n",
    "    name = 'Tiger'\n",
    "    def __init__(self, age, gender,kind):\n",
    "        super().__init__(age, gender)\n",
    "        #self.kind = kind\n",
    "        self.__setattr__('kind1', kind)\n",
    "        #self.__setitem__('kind1', kind)\n",
    "\n",
    "    # def __setitem__(self, key, value):\n",
    "    #     self.__dict__[key] = value\n",
    "\n",
    "    # def __getitem__(self, key):\n",
    "    #     return self.__dict__[key]    \n",
    "        #self.__setitem__('kind1', kind)\n",
    "    \n",
    "    def eat(self):\n",
    "        print(self.name, \"eat other animals\")\n",
    "\n",
    "class Sheep(Animal):\n",
    "    name = 'Sheep'\n",
    "    def __init__(self, age, gender):\n",
    "        super().__init__(age, gender)\n",
    "    \n",
    "    def eat(self):\n",
    "        print(self.name, \"eat grass\")\n",
    "\n",
    "    def __len__(self):\n",
    "        return 10\n",
    "\n",
    "    def __str__(self):\n",
    "        return 'name:{},age:{}, gender:{}'.format(self.name, self.age, self.gender)\n",
    "\n",
    "tiger_one = Tiger(2, 'male', '华南虎')\n",
    "sheep_two = Sheep(1, 'female')\n",
    "\n",
    "tiger_one.eat()\n",
    "sheep_two.eat()\n",
    "\n",
    "\n",
    "def do_eat(animal):\n",
    "    animal.eat()\n",
    "\n",
    "do_eat(tiger_one)\n",
    "do_eat(sheep_two)\n",
    "\n",
    "print(Animal.can_move(10, 20))\n",
    "\n",
    "#tiger_one['kind1'] ='other'\n",
    "#print(tiger_one['kind1'])\n",
    "print('tiger_one properties are:', tiger_one.__dict__)\n",
    "print(sheep_two)\n",
    "print('len of sheep is:', len(sheep_two))\n",
    "#print(tiger_one.__dict__)\n",
    "\n",
    "##TODO\n",
    "#@staticmethod\n",
    "#@classmethod\n",
    "#多态\n",
    "#dict，str()，运算符的重载\n",
    "#setitem,getitem,call()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__setitem__, __getitem__, __setattr__, __getattr__\n",
    "\n",
    "__str__,__repr__\n",
    "\n",
    "def __len__(self):\n",
    "        return len(self.idxs)\n",
    "\n",
    "__iter__,__next__\n",
    "\n",
    "__call__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__iter__ called\n",
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "class Fibonacci:\n",
    "    def __init__(self, max):\n",
    "        self.max = max\n",
    "    def __iter__(self):\n",
    "        print('__iter__ called')\n",
    "        self.a = 0\n",
    "        self.b = 1\n",
    "        return self\n",
    "    def __next__(self):\n",
    "        #print('__next__ called')\n",
    "        fib = self.a\n",
    "        if fib > self.max:\n",
    "            raise StopIteration\n",
    "        self.a, self.b = self.b, self.a + self.b\n",
    "        return fib\n",
    "\n",
    "for i in Fibonacci(60):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CLanguage:\n",
    "    # 定义__call__方法\n",
    "    def __call__(self,name,add):\n",
    "        print(\"调用__call__()方法\",name,add)\n",
    "clangs = CLanguage()\n",
    "clangs(\"关注kindlytree一起学编程\",\"https://space.bilibili.com/505620745\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
